import { getUnitListTree } from '@/services/unit';

function formatTree(list, type, isOnlyOnline) {
    if (!list || list.length === 0) {
        return []
    }

    let newList = []
    list.forEach((value) => {
        let title = value.name
        if (value.type === 1 && !value.name) {
            title = '未分配人员';
        }
        let node = {
            title: title,
            key: value.id,
            value: value[type],
            type: value.type ? value.type : 0,
            children: formatTree(value.children, type, isOnlyOnline)
        }
        if (value.type === 1) {
            node.sn = value.sn ? value.sn : '';
            node.isOnline = value.isOnline ? value.isOnline : 0;
            node.isStreamOnline = value.isStreamOnline ? value.isStreamOnline : 0;
            node.isDisabled = value.isDisabled ? value.isDisabled : 0;
            node.lng = value.lng
            node.lat = value.lat


            newList.push(node)
        }
        if (!value.type) {
            let _count = getCount(value.children)
            node.total = _count.total;
            node.online = _count.online;
            if(isOnlyOnline == 1 ){
                if (_count.online > 0) {
                    newList.push(node)
                }
            } else {
                newList.push(node)
            }
        }

        // return node;
    });
    return newList;
}

function getCount(list) {
    let total = 0;
    let online = 0;
    (list != undefined && list.length) && list.forEach(value => {
        if (value.type === 1) {
            total++
        }
        if (value.type === 1 && value.isOnline === 1) {
            online++;
        }
        if (value.children) {
            let countObj = getCount(value.children)
            total += countObj.total
            online += countObj.online
        }
    })
    return { total, online };
}

function format(list) {
    let unitUserList = []
    const _parse = list => {
        list.map((item) => {
            unitUserList.push({
                key: item.id,
                title: item.name,
                value: item.code,
                isOnline: item.isOnline ? item.isOnline : 0,
                isStreamOnline: item.isStreamOnline ? item.isStreamOnline : 0,
                isDisabled: item.isDisabled ? item.isDisabled : 0,
                lng: item.lng ? item.lng : 0,
                lat: item.lat ? item.lat : 0,
                type: item.type ? item.type : 0,
                sn: item.sn ? item.sn : ''
            })
            if (item.children) {
                _parse(item.children)
            }
            return []
        })
    }
    _parse(list)
    return unitUserList
}

export default {
    namespace: 'unit',

    state: {
        unitAndUserList: [],
        formatUnitAndUserList: [],
        userCount: {
            onlineUser: 0,
            totalUser: 0
        },
    },

    effects: {
        *fetchUnitAndUser({ payload, callback }, { call, put }) {
            const response = yield call(getUnitListTree, payload);
            yield put({
                type: 'unitAndUser',
                payload: { ...response, isOnline: payload.isOnline || 0 },
            });
            if (response && callback) {
                callback(response);
            }
        },
    },

    reducers: {
        unitAndUser(state, action) {
            let { unitList, onlineDevice, totalDevice } = action.payload.data
            let formatUnitAndUserList = formatTree(unitList, 'code', action.payload.isOnline)
            return {
                ...state,
                unitAndUserList: format(unitList),
                formatUnitAndUserList,
                userCount: {
                    onlineUser: onlineDevice,
                    totalUser: totalDevice
                }
            };
        },
    },
};
